/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package apus.entity;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Objects;

/**
 * The <code>RealCall</code> class represents calls. The call is given by 
 * the caller's number and the number receiving the call.
 * @author  Maxim Vasilevsky
 * @author  Roman Dyatkovsky
 * @see     java.text.DateFormat
 * @see     apus.entity.PhoneNumber
 * @since   APUS v0.1
 */
public class RealCall extends AbstractRealEntity implements Call{
    
    /** Number of the calling telephone number. */
    private PhoneNumber numberFrom;
    
    /** Number of the called telephone number. */
    private PhoneNumber numberTo;
    
    /** Duration of call. */
    private int duration;
    
    /** Beginning of the call */
    private Date dateOfCall;

    public RealCall(int id){
        super(id);
    }
    
    /**
     * Initializes a newly created {@code Call} object with an indication 
     * of two number.
     * @param numberFrom calling number.
     * @param numberTo called number.
     */
    public RealCall(PhoneNumber numberFrom, PhoneNumber numberTo) {
        this.numberFrom = numberFrom;
        this.numberTo = numberTo;
    }

    /**
     * Default constructor.
     */
    public RealCall() {
    }
    
    /**
     * Initializes a newly created {@code Call} object with an indication 
     * of two number and date of call.
     * @param numberFrom calling number.
     * @param numberTo called number.
     * @param dateOfCall date of call.
     */
    public RealCall(PhoneNumber numberFrom, PhoneNumber numberTo, Date dateOfCall) {
        this.numberFrom = numberFrom;
        this.numberTo = numberTo;
        this.dateOfCall = dateOfCall;
    }
    
    /**
     * Initializes a newly created {@code Call} object with an indication 
     * of two number, duration and date of call.
     * @param numberFrom The number of caller.
     * @param numberTo The number of called.
     * @param duration The duration of call.
     * @param dateOfCall The date of start call.
     */
    public RealCall(PhoneNumber numberFrom, PhoneNumber numberTo, int duration, Date dateOfCall) {
        this.numberFrom = numberFrom;
        this.numberTo = numberTo;
        this.duration = duration;
        this.dateOfCall = dateOfCall;
    }
    
   public RealCall(PhoneNumber numberFrom, PhoneNumber numberTo, int duration, String dateOfCall) throws ParseException {
        this.numberFrom = numberFrom;
        this.numberTo = numberTo;
        this.duration = duration;
        DateFormat df = DateFormat.getDateInstance();
        this.dateOfCall = df.parse(dateOfCall);
    }

    /**
     * Return the date of the call.
     * @return The date.
     */
    @Override
    public Date getDateOfCall() {
        return dateOfCall;
    }

    /**
     * Return the date of the end of the period.
     * @return number of seconds.
     */
    @Override
    public int getDuration() {
        return duration;
    }

    /**
     * Return the number of caller.
     * @return phone number.
     */
    @Override
    public PhoneNumber getNumberFrom() {
        return numberFrom;
    }

    /**
     * Return the date of called.
     * @return phone number.
     */
    @Override
    public PhoneNumber getNumberTo() {
        return numberTo;
    }


    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final RealCall other = (RealCall) obj;
        if (!Objects.equals(this.numberFrom, other.numberFrom)) {
            return false;
        }
        if (!Objects.equals(this.numberTo, other.numberTo)) {
            return false;
        }
        if (!Objects.equals(this.dateOfCall, other.dateOfCall)) {
            return false;
        }
        return true;
    }
   
    @Override
    public String toString () {
        return "Call from "+ numberFrom + " to "+numberTo+". Date of call: "+ dateOfCall;
    }
    
}